package com.easy.service.impl;

import com.easy.bean.Cart;
import com.easy.dao.CartDao;
import com.easy.dao.ProductDao;
import com.easy.service.ICartService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CartService implements ICartService {
    @Autowired
    CartDao cartDao;

    @Override
    public int add(Cart cart) {
        Cart cart_data=getCart(cart);
        if(cart_data==null){
            cart.setCount(1);
            return cartDao.add(cart);
        }else{
            if(cart_data.getCount()<1){
                cart.setCount(1);
            }
            else if(cart_data.getCount()>100){
                cart.setCount(100);
            }else {
                cart.setCount(cart_data.getCount() + 1);
            }
            return cartDao.edit(cart);
        }
    }

    @Override
    public int edit(Cart cart) {
        return cartDao.edit(cart);
    }

    @Override
    public int delete(int productid,int userid) {
        return cartDao.delete(productid, userid);
    }

    @Override
    public List<Cart> getList(Page page, String text,int userid) {
        return cartDao.getList(page, text,userid);
    }

    @Override
    public Cart getCartById(int productid,int userid) {
        return cartDao.getCartById(productid, userid);
    }

    @Override
    public int getCount(String text,int userid) {
        return cartDao.getCount(text,userid);
    }


    @Override
    public Cart getCart(Cart cart) {
        return cartDao.getCart(cart);
    }

}
